package com.yuanian.springbootProject.controller;

import com.yuanian.springbootProject.dto.ResponseResult;
import com.yuanian.springbootProject.dto.UserRoleName;
import com.yuanian.springbootProject.entity.TUserRole;
import com.yuanian.springbootProject.service.IUserRoleService;
import com.yuanian.springbootProject.util.Utils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("userRole")
public class UserRoleController {

    @Resource
    private IUserRoleService userRoleService;

    /**
     * 插入一个用户角色
     * @param userRole 用户角色数据
     * @return
     */
    @PostMapping("one")
    public ResponseResult insertOne(@RequestBody TUserRole userRole){
        userRole.setId(Utils.getRandomUUID());
        userRole.setCreateDate(new Date());
        userRole.setUpdateDate(new Date());
        int i = userRoleService.insert(userRole);
        if (i > 0) return ResponseResult.ok();
        else return ResponseResult.fail();
    }

    /**
     * 根据id删除一个用户角色
     * @param id
     * @return
     */
    @DeleteMapping("one/{id}")
    public ResponseResult deleteByID(@PathVariable String id){
        return ResponseResult.ok(userRoleService.deleteByPrimaryKey(id));
    }

    /**
     * 更新一个用户角色
     * @param userRole
     * @return
     */
    @PutMapping("one")
    public ResponseResult updateOne(@RequestBody TUserRole userRole){
        return ResponseResult.ok(userRoleService.updateByPrimaryKeySelective(userRole));
    }

    /**
     * 根据id查找一个用户角色
     * @param id
     * @return
     */
    @GetMapping("one/{id}")
    public ResponseResult getOneById(@PathVariable("id") String id){
        return ResponseResult.ok(userRoleService.selectWithNameByPrimaryKey(id));
    }

    /**
     * 查找所有用户角色
     * @return
     */
    @GetMapping("all")
    public ResponseResult getAll(){
        List<UserRoleName> userRoleNames = userRoleService.selectAllWithNames();
        return ResponseResult.ok(userRoleNames);

    }
}
